package com.maduar.demo.netty.http.upload.handler;

import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.http.HttpContentCompressor;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.HttpServerCodec;
import io.netty.handler.stream.ChunkedWriteHandler;
import io.netty.util.concurrent.DefaultEventExecutorGroup;
import io.netty.util.concurrent.EventExecutorGroup;

/**
 * Description:
 *
 * @author danny.pang danny.pang@ebizprise.com
 * @date 2020-03-10 15:26
 */
public class NettyServerInitializer extends ChannelInitializer<SocketChannel> {


    @Override
    public void initChannel(SocketChannel ch) {
        EventExecutorGroup executors = new DefaultEventExecutorGroup(1);

        ChannelPipeline pipeline = ch.pipeline();

        pipeline.addLast(new HttpServerCodec());
        pipeline.addLast(new HttpContentCompressor());

//        pipeline.addLast(new MyDecoder());
//        pipeline.addLast(new StringEncoder(CharsetUtil.UTF_8));
//        pipeline.addLast(new LineBasedFrameDecoder(8192));
//        pipeline.addLast(new StringDecoder(CharsetUtil.UTF_8));

        // 用于上传文件
        pipeline.addLast(executors, new HttpUploadHandler());
        //HTTP 消息的合并处理
        pipeline.addLast("http-chunked", new ChunkedWriteHandler());
    }
}
